
#include "types.h"
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include "logger/Logger.h"
#include "logger/Appender.h"

using namespace std;

class LoggerTest: public CppUnit::TestFixture {
public:
    LoggerTest() {}

	void setUp() {
	}

	void tearDown() {
	}

	bool check (char * fixture, char * value) {
		if (strcmp(fixture, value) != 0) {
			cout << "Comparison failure: expected" << endl;
			cout << "[" << fixture << "] but was" << endl;
			cout << "[" << value << "]" << endl;
			return false;
		}
		return true;
	}

	void testSimple() {
		TestAppender * appender = new TestAppender ();
		Logger * logger = new Logger ();
		logger->setAppender (appender);

		logger->setLevel (LOG_DEBUG);
		logger->debug ("Debug");
		CPPUNIT_ASSERT (check ("Debug", appender->getLastMessage ()));
		logger->info ("Info");
		CPPUNIT_ASSERT (check("Info", appender->getLastMessage ()));
		logger->warn ("Warn");
		CPPUNIT_ASSERT (check("Warn", appender->getLastMessage ()));
		logger->error ("Error");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));

		logger->setLevel (LOG_INFO);
		logger->debug ("Debug");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
		logger->info ("Info");
		CPPUNIT_ASSERT (check("Info", appender->getLastMessage ()));
		logger->warn ("Warn");
		CPPUNIT_ASSERT (check("Warn", appender->getLastMessage ()));
		logger->error ("Error");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));

		logger->setLevel (LOG_WARN);
		logger->debug ("Debug");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
		logger->info ("Info");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
		logger->warn ("Warn");
		CPPUNIT_ASSERT (check("Warn", appender->getLastMessage ()));
		logger->error ("Error");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));

		logger->setLevel (LOG_ERROR);
		logger->debug ("Debug");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
		logger->info ("Info");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
		logger->warn ("Warn");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
		logger->error ("Error");
		CPPUNIT_ASSERT (check("Error", appender->getLastMessage ()));
	}

CPPUNIT_TEST_SUITE( LoggerTest );
	CPPUNIT_TEST( testSimple );
CPPUNIT_TEST_SUITE_END();

};
